package cn.myjerry.server.demo2;

import java.io.IOException;
import java.net.ServerSocket;

public class Server {
	private ServerSocket server;
	
	public static final String CRLF = "\r\n";
	public static final String BLANK = " ";
	
	private boolean isShutDown = false;
	
	public static void main(String[] args) {
		Server server = new Server();
		server.start();
	}
	
	public void start() {
		start(8888);
	}
	
	public void start(int prot) {
		try {
			server = new ServerSocket(prot);
			this.receive();
		} catch (IOException e) {
			stop();
		}
	}
	
	private void receive() {
		try {
			while(!isShutDown) {
				new Thread(new Dispatcher(server.accept())).start();
			}
		} catch (IOException e) {
			stop();
		}
	}
	
	public void stop() {
		this.isShutDown = true;
		CloseUtil.closeAll(server);
	}
}
